package com.clz.appdesign.myapp;

import android.Manifest;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.rxgalleryfinal.RxGalleryFinal;
import cn.finalteam.rxgalleryfinal.bean.ImageCropBean;
import cn.finalteam.rxgalleryfinal.imageloader.ImageLoaderType;
import cn.finalteam.rxgalleryfinal.rxbus.RxBusResultDisposable;
import cn.finalteam.rxgalleryfinal.rxbus.event.ImageRadioResultEvent;
import io.reactivex.functions.Consumer;

public class RxImageActivity extends AppCompatActivity {

    @BindView(R.id.iv_image)
    ImageView mIvImage;
    private RxPermissions mRxPermissions;
    private static String[] PERMISSIONS_NEED = {
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_image);
        ButterKnife.bind(this);
        mRxPermissions = new RxPermissions(this);

    }

    String[] items = new String[]{"拍照", "相册"};

    @OnClick(R.id.btn_select_image)
    public void onViewClicked() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, final int which) {
                mRxPermissions.request(PERMISSIONS_NEED)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean granted) throws Exception {
                                if (granted) {
                                    switch (which) {
                                        case 0:
                                            selectPicture(0);
                                            break;
                                        case 1:
                                            selectPicture(1);
                                            break;
                                        default:
                                            break;
                                    }
                                } else {

                                }
                            }
                        });
                if (which == 0) {

                } else {

                }
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    public void selectPicture(int type) {
        switch (type) {
            case 0:
                RxGalleryFinal.with(this)
                        .image()
                        .crop()
                        .imageLoader(ImageLoaderType.GLIDE)
                        .subscribe(new RxBusResultDisposable<ImageRadioResultEvent>() {

                            @Override
                            protected void onEvent(ImageRadioResultEvent imageRadioResultEvent) throws Exception {
                                final ImageCropBean result = imageRadioResultEvent.getResult();
                                Glide.with(RxImageActivity.this).load(result.getCropPath()).into(mIvImage);
                            }
                        });
                break;
            case 1:

                break;
            default:
                break;
        }
    }
}
